package com.app.teaware.home;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.app.teaware.R;
import com.app.teaware.basis.Constants;
import com.app.teaware.bean.BaseListData;
import com.app.teaware.bean.BaseListDataListBean;
import com.app.teaware.goods.GoodsListActivity;
import com.app.teaware.stock.CheckWareActivity;
import com.app.teaware.util.MyUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import my.ActivityTool;
import my.FileUtils;
import my.LogUtil;
import my.MySharedPreferences;

/**
 * 资讯首页
 */
public class HomeIndexActivity extends HomeBaseActivity {
    public final int GOTOGOODSACT = 48;


    @BindView(R.id.recyclerView_grid)
    RecyclerView recyclerView_grid;
    BaseListDataListBean mListBean;
    AdapterEnter mAdapter;




    @Override
    public void initViews() {
        super.initViews();
        setContentView(R.layout.home_index_activity);
        recyclerView_grid.setLayoutManager(new GridLayoutManager(mContext, 2));
    }

    @Override
    public void initData(Bundle savedInstanceState) {

        super.initData(savedInstanceState);
        initData();
        refreshData();
        FileUtils.deleteFile(Constants.DIR_TEMP);
        setTitle("WMS仓库管理系统");

//        EventBus.getDefault().register(this);
//        UpdateBean.check(mContext, false);
        checkCacheAndDownload();
    }

    private void initData() {
        mListBean = new BaseListDataListBean();
        BaseListData data;
        data = new BaseListData("4","加工入库",R.drawable.icon_home_rk,R.drawable.base_img_default_square);
        mListBean.getList().add(data);
        data = new BaseListData("2","领料管理",R.drawable.icon_home_lj,R.drawable.base_img_default_square);
        mListBean.getList().add(data);
        data = new BaseListData("3","退料管理",R.drawable.icon_home_tj,R.drawable.base_img_default_square);
        mListBean.getList().add(data);
        data = new BaseListData("1","库位盘点",R.mipmap.icon_check_ware,R.drawable.base_img_default_square);
        mListBean.getList().add(data);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
//        EventBus.getDefault().unregister(this);

    }


    @Override
    protected void onResume() {
        super.onResume();
//        QMUIStatusBarHelper.setStatusBarDarkMode(mContext);
    }


    @Override
    public void onDoubleClick() {
        super.onDoubleClick();
        refreshData();
    }

    void refreshData() {
//        getAppEnterGrid();
        setGridEnterView();
    }

//    void getAppEnterGrid() {
//        MyParams params = new MyParams();
//        showProgress();
////        params.put("projectNo", Constants.PROJECT_NO);
//
//        MyHttpUtils.post(Constants.URL_HOME_APP_MODULE, params, new MyHttpListener() {
//                    @Override
//                    public void onSuccess(int httpWhat, Object result) {
//                        mListBeanEnter = (HomeGrideListBean) result;
//                        setGridEnterView();
//                    }
//
//                    @Override
//                    public void onFailure(int httpWhat, Object result) {
////                        super.onFailure(httpWhat, result);
////            mListBean = new ServiceNewListBean();
//                    }
//
//                    @Override
//                    public void onFinish(int httpWhat) {
//                        dismissProgress();
//                    }
//                },
//                0, HomeGrideListBean.class);
//    }

    void setGridEnterView() {
        if(mAdapter == null){
            mAdapter = new AdapterEnter();
            recyclerView_grid.setAdapter(new AdapterEnter());
        }else {
            mAdapter.notifyDataSetChanged();
        }
    }



    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            refreshData();
        }
    }


    public class AdapterEnter extends RecyclerView.Adapter<AdapterEnter.ViewHolder> {

        public AdapterEnter() {

        }

        //创建新View，被LayoutManager所调用
        @Override
        public AdapterEnter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.home_enter_grid_item, viewGroup, false);
            return new AdapterEnter.ViewHolder(view);
        }


        //将数据与界面进行绑定的操作
        @Override
        public void onBindViewHolder(AdapterEnter.ViewHolder viewHolder, int position) {
            viewHolder.bind(position);
        }

        //获取数据的数量
        @Override
        public int getItemCount() {
            return mListBean == null ? 0 : mListBean.getList().size();
        }

        //自定义的ViewHolder，持有每个Item的的所有界面元素
        public class ViewHolder extends RecyclerView.ViewHolder {
            @BindView(R.id.list_item)
            public View list_item;
            @BindView(R.id.text_name)
            public TextView text_name;
            @BindView(R.id.text_count)
            public TextView text_count;
            @BindView(R.id.img_icon)
            public ImageView img_icon;

            public ViewHolder(View view) {
                super(view);
                ButterKnife.bind(this, view);
            }

            public void bind(int position) {
                BaseListData data = mListBean.getList().get(position);
                text_name.setText(data.getName());
                img_icon.setImageResource(data.getPath());
                list_item.setTag(data);
            }

            @OnClick(R.id.list_item)
            public void onItemClick(View v) {
                BaseListData data = (BaseListData) v.getTag();
                MyUtils.putHomeSelectName(data.getName());
                MyUtils.putHomeSelectId(data.getId());
                switch (data.getId()){
                    case "2":
                    case "3":
                    case "4":
                        ActivityTool.skipActivity(mContext, GoodsListActivity.class);
                        break;
                    case "1":
                        ActivityTool.skipActivity(mContext, CheckWareActivity.class);
                        break;
                }

            }
        }
    }


    void checkCacheAndDownload() {
        MySharedPreferences msp = new MySharedPreferences(mContext);
        if (msp.isFirst(mContext)) {
            LogUtil.i("第一次打开  或者 通讯录缓存不存在--直接更新->");
            msp.setFirstDone(mContext);
        } else {
            msp.setFirstDone(mContext);
        }
    }


}
